package threadTimedTask

import (
	"fmt"
	"gowork/shoppingBodyAward/model"
	cron "github.com/robfig/cron/v3"
	"gowork/shoppingBodyAward/utils"
	"sync"
	"time"
)

func init(){
	var lkt *model.LinkageTask
	var lkm *model.LinkageMessage
	var a *model.Account
	var lkfs *model.LinkageFailureSucces
	var cr *model.CallRecord

	w := new(utils.Worker)

	cronLinkage := cron.New()
	threadTask := func() {

		lktList := lkt.QueryLinkageTask()

		var wg sync.WaitGroup
		wg.Add(3)
		for i := 0; i < 3; i++ {
			var finalI int = i
			go func() {
				defer func() {
					// 捕获异常 防止waitGroup阻塞
					if err := recover(); err != nil {
						wg.Done()
					}
				}()

				for _, lktv := range lktList[finalI*3:(finalI+1)*3] {
					if lktv.LinkageState == 1 {
						elkm := lkm.QueryLinkageMessageById(lktv.LinkageMessageId)
						ma := a.QueryAccountById(elkm.RecvAccountId)
						var lkmManager = model.LinkageMessage{
							LinkageMessageId: w.GetId(),
							MessageTypeId:    elkm.MessageTypeId,
							SendAccountId:    elkm.SendAccountId,
							SendAccountName:  elkm.SendAccountName,
							RecvAccountId:    ma.AccountId,
							RecvAccountName:  ma.AccountName,
							CustomerId:       elkm.CustomerId,
							CustomerName:     elkm.CustomerName,
							SendMessageBody:  "请尽快回复！",
							CreatedAt:        time.Now(),
							UpdatedAt:        time.Now(),
						}
						rLkmManager := lkm.InsertLinkageMessage(&lkmManager)
						lktv.LinkageState = 2
						lktv.EmployeeOrSupervisor = 2
						lktv.LinkageMessageId = rLkmManager.LinkageMessageId
						hh, _ := time.ParseDuration("4h")
						lktv.TermOfValidity = lktv.CreatedAt.Add(hh)
						lkt.UpdateTermOfValidity(lktv)

					} else if lktv.LinkageState == 2 {
						lktv.IsOver = 2
						lkt.UpdateTermOfValidity(lktv)
						var newLkfs = model.LinkageFailureSucces{
							LinkageFailureSuccesId: w.GetId(),
							FailureSuccesState:     2,
							CustomerId:             lktv.CustomerId,
							CustomerName:           lktv.CustomerName,
							LinkageTaskId:          lktv.LinkageTaskId,
							CreatedAt:              time.Now(),
							UpdatedAt:              time.Now(),
						}
						lkfs.InsertLinkageFailureSucces(&newLkfs)

					} else if lktv.LinkageState == 3 {
						if len(cr.QueryCallRecord(lktv.CustomerId, lktv.AccountId, lktv.ShoppingReply, lktv.TermOfValidity)) > 0 {
							lktv.IsOver = 2
							lkt.UpdateTermOfValidity(lktv)
							var newLkfs = model.LinkageFailureSucces{
								LinkageFailureSuccesId: w.GetId(),
								FailureSuccesState:     1,
								CustomerId:             lktv.CustomerId,
								CustomerName:           lktv.CustomerName,
								LinkageTaskId:          lktv.LinkageTaskId,
								CreatedAt:              time.Now(),
								UpdatedAt:              time.Now(),
							}
							lkfs.InsertLinkageFailureSucces(&newLkfs)
						} else {
							elkm := lkm.QueryLinkageMessageById(lktv.LinkageMessageId)
							ma := a.QueryAccountById(elkm.SendAccountId)
							var lkmManager = model.LinkageMessage{
								LinkageMessageId: w.GetId(),
								MessageTypeId:    elkm.MessageTypeId,
								SendAccountId:    elkm.SendAccountId,
								SendAccountName:  elkm.SendAccountName,
								RecvAccountId:    ma.AccountId,
								RecvAccountName:  ma.AccountName,
								CustomerId:       elkm.CustomerId,
								CustomerName:     elkm.CustomerName,
								SendMessageBody:  "请尽快给客户致电！",
								CreatedAt:        time.Now(),
								UpdatedAt:        time.Now(),
							}
							rLkmManager := lkm.InsertLinkageMessage(&lkmManager)
							lktv.LinkageState = 4
							lktv.EmployeeOrSupervisor = 2
							lktv.LinkageMessageId = rLkmManager.LinkageMessageId
							hh, _ := time.ParseDuration("36h")
							lktv.TermOfValidity = lktv.CreatedAt.Add(hh)
							lkt.UpdateTermOfValidity(lktv)
						}

					} else if lktv.LinkageState == 4 {
						if len(cr.QueryCallRecord(lktv.CustomerId, lktv.AccountId, lktv.ShoppingReply, lktv.TermOfValidity)) > 0 {
							lktv.IsOver = 2
							lkt.UpdateTermOfValidity(lktv)
							var newLfs = model.LinkageFailureSucces{
								LinkageFailureSuccesId: w.GetId(),
								FailureSuccesState:     1,
								CustomerId:             lktv.CustomerId,
								CustomerName:           lktv.CustomerName,
								LinkageTaskId:          lktv.LinkageTaskId,
								CreatedAt:              time.Now(),
								UpdatedAt:              time.Now(),
							}
							lkfs.InsertLinkageFailureSucces(&newLfs)
						} else {
							lktv.IsOver = 2
							lkt.UpdateTermOfValidity(lktv)
							var newLfs = model.LinkageFailureSucces{
								LinkageFailureSuccesId: w.GetId(),
								FailureSuccesState:     2,
								CustomerId:             lktv.CustomerId,
								CustomerName:           lktv.CustomerName,
								LinkageTaskId:          lktv.LinkageTaskId,
								CreatedAt:              time.Now(),
								UpdatedAt:              time.Now(),
							}
							lkfs.InsertLinkageFailureSucces(&newLfs)
						}
					}

					fmt.Println("zzzzzzzz......", finalI)
				}

				wg.Done()
			}()
		}

		wg.Wait()
	}

	// 添加定时任务, * * * * * 是 crontab,表示每分钟执行一次
	cronLinkage.AddFunc("* * * * *", threadTask)
	// 启动定时器
	cronLinkage.Start()
}
